package java21demo;

import java.util.*;

public class SequentialCollectionsTest {
    public static void sequentialArrayListTest() {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.addFirst("begin");
        list.addLast("end"); // 同list.add("end")
        for(String e: list){ // 输出：begin a b c end
            System.out.print(e+" ");
        }
        System.out.println();
        List<String> reversed = list.reversed();
        for(String e: reversed){ // 输出：end c b a begin
            System.out.print(e+" ");
        }
        System.out.println();
    }

    public static void sequentialTreeSetTest(){
        TreeSet<String> set = new TreeSet<>();
        set.add("b");
        set.add("a");
        set.add("c");
        //set.addFirst("begin"); // UnsupportedOperationException，因为TreeSet中元素的位置由比较顺序决定
        // set.addLast("end"); // UnsupportedOperationException，因为TreeSet中元素的位置由比较顺序决定
        for(String e: set){ // 输出：a b c
            System.out.print(e+" ");
        }
        System.out.println();
        Set<String> reversed = set.reversed();
        for(String e: reversed){ // 输出：c b a
            System.out.print(e+" ");
        }
        System.out.println();
    }

    public static void sequentialLinkedHashMapTest(){
        LinkedHashMap<String,String> map = new LinkedHashMap<>();
        map.put("a","1");
        map.put("b","2");
        map.put("c","3");
        map.putFirst("d","4");
        map.putLast("e","5");
        System.out.println("Map EntrySet：");
        for(Map.Entry<String,String> e: map.entrySet()){
            System.out.println(e.getKey()+" "+e.getValue());
        }
        System.out.println("Reversed Map EntrySet：");
        SequencedMap<String, String> reversed = map.reversed();
        for(Map.Entry<String,String> e:map.reversed().entrySet()){
            System.out.println(e.getKey()+" "+e.getValue());
        }
    }
    public static void main(String[] args) {
        sequentialArrayListTest();
        sequentialTreeSetTest();
        sequentialLinkedHashMapTest();
    }
}
